﻿namespace FSLib

type FilterStyleBlueSky() =
    interface IFilter2D with

        member this.FilterName = "FilterStyleBlueSky"
        member this.Description = "空をより青くする"

        member this.Compute colmat:int[,] =

            // 水色～青系の色かどうか判断
            let isBlue (h,s,v) = (150.0 <= h && h <= 250.0)

            // 鮮やかにした彩度を計算（HUEの200度に近いほど鮮やかにする）
            let improveSat (h,s,v) =
                let hueRange = abs (200.0 - h) // Max=50
                let addSat = 
                    match hueRange with
                    | i when 0.0<=i && i<=1.0 -> 0.2
                    | _ -> s + (0.2 * 1.0/hueRange)
                let is = if s+addSat > 1.0 then 1.0 else s+addSat
                (h,is,v)

            // 青系の彩度を強くする
            let bluesky col =
                let (r,g,b) = CommonUtils.toRGBF(col)
                let (h,s,v) = CommonUtils.rgb2hsv(r,g,b)

                match isBlue(h,s,v) with
                    | false -> CommonUtils.toValue(int r, int g, int b)
                    | true  -> 
                        let (ih,is,iv) = improveSat(h,s,v)
                        let (ir,ig,ib) = CommonUtils.hsv2rgb(ih,is,iv)
                        CommonUtils.toValue(int ir, int ig, int ib)

            Array2D.map bluesky colmat
